<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <div class="h_top">
          <div class="bread_title">
            <span>更新设备启停</span>
          </div>
          <div class="save_button">
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-document-checked" size="mini" @click="save">保存</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-document-remove" size="mini" @click="confirmDel">删除</el-button>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <div>
      <el-row>
        <el-col :span="24">
          <el-form ref="form" :model="data" :inline="true" :rules="formRules">
            <el-row :gutter="10">
              <el-col :span="4">
                <el-form-item prop="coding">
                  <div>编号</div>
                  <el-input v-model="data.coding" disabled size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="name">
                  <div>名称</div>
                  <el-input v-model="data.name" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="equipment_id">
                  <div>资产</div>
                  <v-select v-model="data.equipment_id" action="eqList" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item>
                  <div>规格型号</div>
                  <el-input v-model="data.eq.spec" size="mini" disabled />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item>
                  <div>使用部门</div>
                  <el-input v-model="data.eq.department.name" size="mini" disabled />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item>
                  <div>制造厂家</div>
                  <el-input v-model="data.eq.manufacturer.name" size="mini" disabled />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="turn">
                  <div>操作</div>
                  <template>
                    <el-radio v-model="data.turn" label="1">启用</el-radio>
                    <el-radio v-model="data.turn" label="2">停用</el-radio>
                  </template>
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="date">
                  <div>预计日期</div>
                  <el-date-picker v-model="data.date" type="datetime" placeholder="选择日期时间" value-format="yyyy-MM-dd HH:mm:ss" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="status">
                  <div>状态</div>
                  <el-select v-model="data.status" size="mini" :disabled="data.statusChange">
                    <el-option v-for="(item, index) in turnStatus" :key="index" :value="index" :label="item" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="old_value">
                  <div>原值</div>
                  <el-input v-model="data.old_value" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="depreciation">
                  <div>已提折旧</div>
                  <el-input v-model="data.depreciation" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="new_value">
                  <div>净值</div>
                  <el-input v-model="data.new_value" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="sort">
                  <div>排序</div>
                  <el-input v-model="data.sort" size="mini" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <el-tabs>
            <el-tab-pane label="原因">
              <el-row :gutter="10">
                <el-col :span="24">
                  <el-input v-model="data.reason" :autosize="{ minRows: 12, maxRows: 14}" type="textarea" :rows="2" placeholder="请输入内容" :disabled="data.statusChange" />
                </el-col>
              </el-row>
            </el-tab-pane>
            <el-tab-pane label="使用部门意见">
              <el-row :gutter="10">
                <el-col :span="24">
                  <el-input v-model="data.shiyong" :autosize="{ minRows: 12, maxRows: 14}" type="textarea" :rows="2" placeholder="请输入内容" />
                </el-col>
              </el-row>
            </el-tab-pane>
            <el-tab-pane label="归口管理部门意见">
              <el-row :gutter="10">
                <el-col :span="24">
                  <el-input v-model="data.guikou" :autosize="{ minRows: 12, maxRows: 14}" type="textarea" :rows="2" placeholder="请输入内容" />
                </el-col>
              </el-row>
            </el-tab-pane>
            <el-tab-pane label="资产管理部门意见">
              <el-row :gutter="10">
                <el-col :span="24">
                  <el-input v-model="data.guanli" :autosize="{ minRows: 12, maxRows: 14}" type="textarea" :rows="2" placeholder="请输入内容" />
                </el-col>
              </el-row>
            </el-tab-pane>
            <el-tab-pane label="描述">
              <el-row :gutter="10">
                <el-col :span="4">
                  <div>
                    <span>创建时间日期</span>
                    <el-input v-model="data.create_time" placeholder="创建时间日期" class="mtb10" size="mini" :disabled="false" />
                  </div>
                  <div>
                    <span>创建人</span>
                    <el-input v-model="data.cr_admin.name" placeholder="创建人" class="mtb10" size="mini" :disabled="true" />
                  </div>
                  <div v-if="data.update_time">
                    <div>
                      <span>最近更新</span>
                      <el-input v-model="data.update_time" placeholder="最近更新" class="mtb10" size="mini" :disabled="true" />
                    </div>
                    <div>
                      <span>更新者</span>
                      <el-input v-model="data.up_admin.name" placeholder="更新者" class="mtb10" size="mini" :disabled="true" />
                    </div>
                  </div>
                </el-col>
                <el-col :span="20">
                  <wangeditor id="content" v-model="data.content" />
                </el-col>
              </el-row>
            </el-tab-pane>
          </el-tabs>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
<script>
import API from '@/api/asset/turn'
import { typeCategory, turnStatus } from '@/utils/options'
import Wangeditor from '@/components/editor/wangeditor'
import VSelect from '@/components/select'
import { mapGetters } from 'vuex'
export default {
  inject: ['backUrl', 'delOne', 'update'],
  components: {
    Wangeditor,
    VSelect
  },
  data() {
    return {
      data: {
        eq: {
          spec: '',
          department: {
            name: ''
          },
          manufacturer: {
            name: ''
          }
        },
        admin: {
          phone: ''
        },
        cr_admin: {
          'name': ''
        }
      },
      typeCategory,
      turnStatus,
      formRules: {
        coding: [
          {
            required: true,
            message: '请输入编号',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '请输入名称',
            trigger: 'blur'
          }
        ],
        equipment_id: [
          {
            required: true,
            message: '请选择资产',
            trigger: 'change'
          }
        ],
        date: [
          {
            required: true,
            message: '请输入预计日期',
            trigger: 'blur'
          }
        ],
        old_value: [
          {
            required: true,
            message: '请输入原值',
            trigger: 'blur'
          }
        ],
        depreciation: [
          {
            required: true,
            message: '请输入已提折旧',
            trigger: 'blur'
          }
        ],
        new_value: [
          {
            required: true,
            message: '请输入净值',
            trigger: 'blur'
          }
        ],
        sort: [
          {
            required: true,
            message: '请输入排序',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'roles'
    ])
  },
  watch: {},
  created() {
    const id = this.$route.params.id
    API.getOne(id).then(response => {
      if (response.data.error_code === 27404) {
        this.$alert('您请求的启停单不存在', '错误', {
          confirmButtonText: '确定',
          callback: () => {
            this.backUrl()
          }
        })
      }
      this.data = response.data
      this.data.statusChange = true
      this.setRole()
    })
  },
  methods: {
    confirmDel() {
      const id = this.data.id
      this.delOne(API, id, true)
    },
    save() {
      const thisData = this.data
      const thisForm = this.$refs.form
      this.update(API, thisData, thisForm)
    },
    setRole() {
      if (this.roles.includes(128)) {
        this.data.statusChange = false
      }
    }
  }
}
</script>
